from django.db import models
from datetime import datetime


# 部门表
class Department(models.Model):
    department_name = models.CharField(verbose_name="部门名称", max_length=18)
    parent = models.IntegerField(verbose_name="部门层级",  default=0)
    remark = models.CharField(verbose_name="备注", max_length=128, null=True, default="")


# 职级
class Rank(models.Model):
    rank_name = models.CharField(verbose_name="职级名称", max_length=26)
    remark = models.CharField(verbose_name="备注", max_length=128, null=True, default="")


# 岗位
class Position(models.Model):
    position_name = models.CharField(verbose_name="岗位名称", max_length=36)
    remark = models.CharField(verbose_name="备注", max_length=128, null=True, default="")


# 岗位职级多对多
class RankPosition(models.Model):
    position_id = models.ForeignKey(Position, on_delete=models.CASCADE, verbose_name="关联岗位", db_constraint=False)
    rank = models.ForeignKey(Rank, on_delete=models.CASCADE, verbose_name="关联职级", db_constraint=False)


# 角色
class Role(models.Model):
    role_name = models.CharField(verbose_name="角色名", max_length=20)
    remark = models.CharField(verbose_name="备注", max_length=128)
    menu_me_urls = models.TextField(verbose_name="菜单路由地址",  default="")


# 菜单
class Menu(models.Model):
    menu_name = models.CharField(verbose_name="菜单名称", max_length=10)
    menu_url = models.CharField(verbose_name="菜单路由", max_length=50)
    menu_icon = models.CharField(verbose_name="菜单图标", max_length=64, null=True, default="")
    menu_me_id = models.IntegerField(verbose_name="自关联菜单")
    remark = models.CharField(verbose_name="备注", max_length=128)
    sort = models.IntegerField(verbose_name="排序")


# 角色菜单多对多
class RoleMenu(models.Model):
    role_id = models.ForeignKey(Role, on_delete=models.CASCADE, verbose_name="角色id", db_constraint=False)
    menu_id = models.ForeignKey(Menu, on_delete=models.CASCADE, verbose_name="菜单id", db_constraint=False)


# 用户
class User(models.Model):
    user_name = models.CharField(verbose_name="用户名", max_length=52, default="会飞的猪")
    user_account = models.CharField(verbose_name="账号", max_length=32)
    user_password = models.CharField(verbose_name="密码", max_length=128, default="123456")
    mobile = models.CharField(verbose_name="手机号", max_length=11)
    role_id = models.ForeignKey(Role, on_delete=models.CASCADE, verbose_name="角色id", db_constraint=False, )
    position_id = models.ForeignKey(Position, on_delete=models.CASCADE, verbose_name="岗位id", db_constraint=False)
    rank_id = models.ForeignKey(Rank, on_delete=models.CASCADE, verbose_name="职级id", db_constraint=False)
    department_id = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name="部门id", db_constraint=False)


# 登录日志
class LoginLog(models.Model):
    ip = models.CharField(verbose_name="登录ip", max_length=52)
    login_name = models.CharField(verbose_name="账号名称", max_length=52)
    device = models.CharField(verbose_name="登录设备", max_length=64)
    place = models.CharField(verbose_name="登录地点", max_length=64)
    login_time = models.DateTimeField(verbose_name="登录时间", default=datetime.now)


# 操作日志
class OperationLog(models.Model):
    operation_name = models.CharField(verbose_name="操作人", max_length=52)
    operation_url = models.CharField(verbose_name="操作路径", max_length=64)
    operation_action = models.CharField(verbose_name="操作动作", max_length=64)
    operation_time = models.DateTimeField(verbose_name="操作时间", default=datetime.now)


# 模板名
class TemplateSheet(models.Model):
    template_name = models.CharField(verbose_name="模板名称", max_length=64)


# 审批类型
class ApprovalType(models.Model):
    approval_name = models.CharField(verbose_name="类型名称", max_length=64)
    last_edit = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="最后编辑人", db_constraint=False)
    last_time = models.DateTimeField(verbose_name="最后修改时间", default=datetime.now)
    template_id = models.ForeignKey(TemplateSheet, on_delete=models.CASCADE, verbose_name="模板id", db_constraint=False)
    approval_title = models.CharField(verbose_name="审批表名", max_length=64)


# 审批流程类型
class ApprovalProcess(models.Model):
    approvalProcess_type = models.ForeignKey(ApprovalType, on_delete=models.CASCADE, verbose_name="流程类型",
                                             db_constraint=False)
    status = models.IntegerField(verbose_name="0:禁用1启用", default=1)
    approvalProcess_illustrate = models.CharField(verbose_name="流程说明", max_length=64)


# 审批人流程表
class ApprovalProcessUser(models.Model):
    approvalProcess_id = models.ForeignKey(ApprovalProcess, on_delete=models.CASCADE, verbose_name="流程id",
                                           db_constraint=False)
    approval_user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="审批人", db_constraint=False)
    num_user = models.IntegerField(verbose_name="第几级审批人")


# 审批表
class Approval(models.Model):
    applicant = models.CharField(verbose_name="申请人", max_length=64)
    applicant_user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="申请人id", db_constraint=False)
    approval_type = models.ForeignKey(ApprovalType, on_delete=models.CASCADE, verbose_name="审批类型", db_constraint=False)
    approval_time = models.DateTimeField(verbose_name="申请时间", default=datetime.now)
    now_approval = models.IntegerField(verbose_name="当前该谁审批")
    now_approval_name = models.IntegerField(verbose_name="当前展示审批人")
    status = models.IntegerField(verbose_name="1:已通过,2:审批中,3:待审批,4:已撤销,0:已驳回")
    reason = models.CharField(verbose_name="理由", max_length=64)


# 审批表请假
class LeaveApproval(models.Model):
    approval_id = models.ForeignKey(Approval, on_delete=models.CASCADE, verbose_name="审批基本id",
                                    db_constraint=False,  related_name="leave")
    begin_time = models.DateTimeField(verbose_name="开始请假时间")
    over_time = models.DateTimeField(verbose_name="结束请假时间")


# 借款审批表
class LoanApproval(models.Model):
    approval_id = models.ForeignKey(Approval, on_delete=models.CASCADE, verbose_name="审批基本id",
                                    db_constraint=False, related_name="loan")
    begin_time = models.DateTimeField(verbose_name="借款日期")
    loan_reason = models.CharField(verbose_name="借款原因", max_length=64)
    loan_money = models.FloatField(verbose_name="借款金额")


# 审批人
class ApprovalUser(models.Model):
    approval_user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="审批人", db_constraint=False)
    approval_id = models.ForeignKey(Approval, on_delete=models.CASCADE, verbose_name="审批id", db_constraint=False)
    flag = models.IntegerField(verbose_name="审批级别")
    is_approval = models.IntegerField(verbose_name="是否审批过", default=0)


# 材料
class Material(models.Model):
    material_name = models.CharField(verbose_name="材料名称", max_length=64)
    material_url = models.CharField(verbose_name="材料地址", max_length=1024)


# 工资
class Salary(models.Model):
    name = models.CharField(verbose_name="姓名", max_length=64)
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户id", db_constraint=False)
    attendance = models.IntegerField(verbose_name="出勤")
    industrial_mining = models.IntegerField(verbose_name="旷工")
    ask_leave = models.IntegerField(verbose_name="请假")
    basic_wage = models.FloatField(verbose_name="基本工资")
    wage_jobs = models.FloatField(verbose_name="岗位工资")


# 操作日志
class OpLogs(models.Model):
    id = models.AutoField(primary_key=True)
    re_time = models.CharField(max_length=32, verbose_name='请求时间')
    re_user = models.CharField(max_length=32, verbose_name='操作人')
    re_ip = models.CharField(max_length=32, verbose_name='请求IP')
    re_url = models.CharField(max_length=255, verbose_name='请求url')
    re_method = models.CharField(max_length=11, verbose_name='请求方法')
    re_content = models.TextField(null=True, verbose_name='请求参数')
    rp_content = models.TextField(null=True, verbose_name='响应参数')
    access_time = models.IntegerField(verbose_name='响应耗时/ms')

    class Meta:
        db_table = 'op_logs'
